<?php

namespace app\shop\validate;

class UserValidate extends BaseValidate
{
    protected $rule = [
        'file'               => 'require|file|image|fileSize:2097152',
        'pay_way'            => 'require|in:alipay,wxpay',
        'old_password'       => 'require|validatePassword',
        'new_password'       => 'require|validatePassword',
        'sms_captcha_code'   => 'require',
        'password'           => 'require',
        'phone'              => 'require|mobile',
        'type'               => 'require',
        'verification_token' => 'require',
    ];

    protected $message = [
        'file.require'                  => '请上传头像文件',
        'file.file'                     => '头像文件格式不正确',
        'file.image'                    => '头像文件格式不正确',
        'file.fileSize'                 => '头像文件大小不能超过2M',
        'pay_way.require'               => '请选择支付方式',
        'pay_way.in'                    => '支付方式不正确',
        'password.require'              => '密码不能为空',
        'old_password.require'          => '旧密码不能为空',
        'new_password.require'          => '新密码不能为空',
        'password.validatePassword'     => '密码格式有误',
        'old_password.validatePassword' => '旧密码格式有误',
        'new_password.validatePassword' => '新密码格式有误',
        'sms_captcha_code.require'      => '短信验证码不能为空',
        'phone.require'                 => '手机号不能为空',
        'phone.mobile'                  => '手机号格式不正确',
        'type.require'                  => '验证类型不能为空',
        'verification_token.require'    => '验证凭证不能为空'
    ];

    protected $scene = [
        'edit'              => [''],
        'avatar'            => ['file'],
        'recharge'          => ['pay_way'],
        'change_password'   => ['old_password', 'new_password'],
        'reset_password'    => ['new_password', 'sms_captcha_code'],
        'verification'      => ['password', 'sms_captcha_code'],
        'change_bind_phone' => ['phone', 'sms_captcha_code', 'verification_token']
    ];

    public function validatePassword($value): bool
    {
        return (bool)preg_match('/^[^&<>\"]{6,32}$/', $value);
    }
}
